﻿/********************************************************************
	created:	2011/05/29
	created:	29:5:2011   21:09
	filename: 	D:\My Documents\Visual Studio 2010\Projects\IntranetMessagerClient\Client\ClientChatForm.cs
	file path:	D:\My Documents\Visual Studio 2010\Projects\IntranetMessagerClient\Client
	file base:	ClientChatForm
	file ext:	cs
	author:		
	
	purpose:	聊天窗口功能实现
*********************************************************************/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using CommonLibrary;

namespace Client
{
    public sealed partial class ClientChatForm : Form
    {
        //when we need change some property in another thread, used this parameter;
        private readonly SynchronizationContext _syncContext;

        private readonly List<MessageContent> _thisChatWindowMessages;

        private readonly string _thisChatWindowMyName;
        private readonly int _thisChatWindowMyUid;
        private readonly string _thisChatWindowReceiverName;
        private readonly int _thisChatWindowReceiverUid;

        public ClientChatForm(int thisChatWindowMyUid, int thisChatWindowReceiverUid, string thisChatWindowMyName,
                              string thisChatWindowReceiverName)
        {
            InitializeComponent();
            _thisChatWindowMyUid = thisChatWindowMyUid;
            _thisChatWindowReceiverUid = thisChatWindowReceiverUid;
            _thisChatWindowMyName = thisChatWindowMyName;
            _thisChatWindowReceiverName = thisChatWindowReceiverName;
            _thisChatWindowMessages = new List<MessageContent>();
            Text = @"正在与 " + _thisChatWindowReceiverName + @" 聊天";
            _syncContext = SynchronizationContext.Current;
        }

        public void AddNewMessage(string senderName, string message, DateTime sendTime)
        {
            if (!richTextBoxChatMessage.InvokeRequired)
            {
                _thisChatWindowMessages.Add(new MessageContent
                                                {
                                                    Message = message,
                                                    Sender = senderName,
                                                    Time = sendTime
                                                });
                richTextBoxChatMessage.AppendText(string.Format("{0}  {1}{2}{3}{4}", senderName, sendTime,
                                                                Environment.NewLine, message, Environment.NewLine));
            }
            else
            {
                _thisChatWindowMessages.Add
                    (new MessageContent
                         {
                             Message = message,
                             Sender = senderName,
                             Time = sendTime
                         });
                _syncContext.Post(msg => richTextBoxChatMessage.AppendText(string.Format("{0}  {1}{2}{3}{4}", senderName,
                                                                                        sendTime.ToString(),
                                                                                        Environment.NewLine, message,
                                                                                        Environment.NewLine))
                                 , null);
            }
        }

        private void ButtonSentClick(object sender, EventArgs e)
        {
            if (richTextBoxChatEdit.Text == null)
                return;
            var talkingPackage = new Protocol.TalkingPackage
                                     {
                                         Receiver = _thisChatWindowReceiverUid,
                                         Sender = _thisChatWindowMyUid,
                                         ReceiverName = _thisChatWindowReceiverName,
                                         SenderName = _thisChatWindowMyName,
                                         MyUid = _thisChatWindowMyUid,
                                         MyName = _thisChatWindowMyName,
                                         Message = richTextBoxChatEdit.Text,
                                         SendTime = DateTime.Now
                                     };
            byte[] messageSend = CommonFunctionClass.SealMessageToEnvelope(talkingPackage, talkingPackage.MyUid, true,
                                                                           false);
            MainWindow.SendDataDelegateFunc.BeginInvoke(messageSend, null, null);
            _thisChatWindowMessages.Add(new MessageContent
                                            {
                                                Message = talkingPackage.Message,
                                                Sender = talkingPackage.MyName,
                                                Time = talkingPackage.SendTime
                                            });
            richTextBoxChatEdit.Text = null;
            richTextBoxChatMessage.AppendText(string.Format("{0}  {1}{2}{3}{4}", talkingPackage.MyName,
                                                            talkingPackage.SendTime, Environment.NewLine,
                                                            talkingPackage.Message, Environment.NewLine));
        }

        private void ButtonCloseChatClick(object sender, EventArgs e)
        {
            Hide();
        }

        #region Nested type: MessageContent

        private class MessageContent
        {
            public DateTime Time { get; set; }
            public string Message { get; set; }
            public string Sender { get; set; }
        }

        #endregion
    }
}